package com.prs.crm.action.sale.flow;

import java.util.Collection;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.flow.WorkFlowProcessNodeDetail;
import com.prs.crm.domain.sale.SalesStoreOut;
import com.prs.crm.domain.sale.SalesStoreOutDetail;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.service.sale.SalesStoreOutService;
import com.prs.crm.service.stock.ProductStoreService;

@SuppressWarnings("serial")
public class SalesStoreOutFlowAction extends FlowAction<SalesStoreOut> {
	
	private SalesStoreOut salesStoreOut;
	private SalesStoreOutService salesStoreOutService;	
	private PaginationSupport pagination;
	private WorkFlowProcessNodeDetail detail;
	@Autowired
	private ProductStoreService productStoreService;
	
	private Collection<SalesStoreOutDetail> slist;
	
	
	
	@Action("listSalesStoreOutFlow")
	public String listSalesStoreOutFlow() {
		pagination = this.getPaginationHelper().prepare(pagination);
		pagination = this.getSalesStoreOutService().listFlowTask(SalesStoreOut.class, pagination, AppContext.getAccountContext().currentUser());
		return SUCCESS;
	}
	
	@Action("editSalesStoreOutFlow")
	public String editSalesStoreOutFlow() {
		this.displayEndNodes();		
		return SUCCESS;
	}
	
	@Clew(results={"success"})
	@Action(value="submitSalesStoreOutFlow",results={@Result(name="success",type="redirect",location="listSalesStoreOutFlow")})
	public String submitSalesStoreOutFlow() {
		if(detail.getResult()==4){
			salesStoreOut = this.getSalesStoreOutService().get(salesStoreOut.getId());
			this.slist =  salesStoreOut.getDetails();
			for(int i=0;i<slist.size();i++){
			SalesStoreOutDetail ds=slist.iterator().next();	
			ProductStore psObject = this.getProductStoreService().getStore(salesStoreOut.getStoreRoom(),ds.getProduct(),ds.getProduct().getBatchNum());
			psObject.setProductNum(psObject.getProductNum() +ds.getShippingNum());
			}
		}
		this.getSalesStoreOutService().execute(this.getNode(), this.getDetail(), salesStoreOut, AppContext.getAccountContext().currentUser());
		this.setPrompt("提交成功");
		return SUCCESS;
	}

	
	
	

	

	
	public Collection<SalesStoreOutDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<SalesStoreOutDetail> slist) {
		this.slist = slist;
	}

	public SalesStoreOut getSalesStoreOut() {
		return salesStoreOut;
	}

	public void setSalesStoreOut(SalesStoreOut salesStoreOut) {
		this.salesStoreOut = salesStoreOut;
	}

	public SalesStoreOutService getSalesStoreOutService() {
		return salesStoreOutService;
	}

	public void setSalesStoreOutService(SalesStoreOutService salesStoreOutService) {
		this.salesStoreOutService = salesStoreOutService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public WorkFlowProcessNodeDetail getDetail() {
		return detail;
	}

	public void setDetail(WorkFlowProcessNodeDetail detail) {
		this.detail = detail;
	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}
	
	
	
	
	
	
	

}
